﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

namespace INFTUR
{
    public partial class ManterPacote : System.Web.UI.Page
    {
        public static bool controle = false;

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Limpar()
        {
            tbID.Text = "";
            tbID.Enabled = false;
            tbNome.Text = "";
            tbNome.Enabled = false;
            tbDescricao.Text = "";
            tbDescricao.Enabled = false;
            tbDataIni.Text = "";
            tbDataIni.Enabled = false;
            tbDataFim.Text = "";
            tbDataFim.Enabled = false;
            
            
            dlDestino.SelectedValue = "0";
            dlDestino.Enabled = false;
            dlHotel.SelectedValue = "0";
            dlHotel.Enabled = false;
            
            tbParcelamento.Text = "";
            tbParcelamento.Enabled = false;
            tbPreco.Text = "";
            tbPreco.Enabled = false;
            btGravar.Enabled = false;
            btCancelar.Enabled = false;
            lbErro.Text = "";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            tbID.Enabled = false;          
            tbNome.Enabled = true;         
            tbDescricao.Enabled = true;          
            tbDataIni.Enabled = true;          
            tbDataFim.Enabled = true;          
            tbParcelamento.Enabled = true;           
            tbPreco.Enabled = true;
            btGravar.Enabled = true;
            btCancelar.Enabled = true;
            dlDestino.Enabled = true;
            dlHotel.Enabled = true;
            controle = true;

        }

        protected void btCancelar_Click(object sender, EventArgs e)
        {
            Limpar();
        }

        protected void btGravar_Click(object sender, EventArgs e)
        {
            
                try
                {

                    PacoteBO pBO = new PacoteBO();
                    pBO.Nome = tbNome.Text;
                    pBO.Parcelamento = tbParcelamento.Text;
                    if (tbPreco.Text.Equals(""))
                    {
                        pBO.Preco = 0;
                    }
                    else
                    {
                        pBO.Preco = Convert.ToDouble(tbPreco.Text);
                    }
                    pBO.DataInicio = Convert.ToDateTime(tbDataIni.Text);
                    pBO.DataFim = Convert.ToDateTime(tbDataFim.Text);
                    pBO.Descricao = tbDescricao.Text;
                    HotelBO hBo = new HotelBO();
                    hBo.Id = Convert.ToInt32(dlHotel.SelectedValue);
                    
                    pBO.Hotel = hBo;
                    DestinoBO dBO = new DestinoBO();
                    dBO.Id = Convert.ToInt32(dlDestino.SelectedValue);
                  //  dBO.Id = 1;
                    pBO.Destino = dBO;

                    PacoteBOMetodos pBOMetodos = new PacoteBOMetodos();
                    if (controle == true)
                    {
                        pBOMetodos.inserir(pBO);
                        Limpar();
                        GridView1.DataBind();
                    }
                    else
                    {
                        pBO.Id = Convert.ToInt32(tbID.Text);
                        pBOMetodos.atualizar(pBO);
                        Limpar();
                        GridView1.DataBind();
                    }
                }
                catch (Exception ex)
                {
                    lbErro.Text = ex.Message;
                }
            
            
        }

        protected void lbAtualizar_Click(object sender, EventArgs e)
        {
            controle = false;
            LinkButton lb = ((LinkButton)sender);
            DataControlFieldCell celula = (DataControlFieldCell)lb.Parent;
            GridViewRow linha = (GridViewRow)celula.Parent;
            
            int id = Convert.ToInt32(linha.Cells[0].Text);

            PacoteBOMetodos pBO = new PacoteBOMetodos();
            
            PacoteBO p = new PacoteBO();
            p = pBO.consultar(id);
            tbID.Text = Convert.ToString(p.Id);
            tbNome.Text = p.Nome;
            tbDescricao.Text = p.Descricao;
            tbDataIni.Text = Convert.ToString(p.DataInicio); 
            tbDataFim.Text = Convert.ToString(p.DataFim);
            tbPreco.Text = Convert.ToString(p.Preco);
            tbParcelamento.Text = Convert.ToString(p.Parcelamento);
            dlDestino.SelectedValue = Convert.ToString(p.Destino.Id);
            dlHotel.SelectedValue = Convert.ToString(p.Hotel.Id);

            tbID.Enabled = true;
            tbNome.Enabled = true;
            tbDescricao.Enabled = true;
            tbDataIni.Enabled = true;
            tbDataFim.Enabled = true;
            tbParcelamento.Enabled = true;
            tbPreco.Enabled = true;
            btGravar.Enabled = true;
            btCancelar.Enabled = true;
            dlDestino.Enabled = true;
            dlHotel.Enabled = true;

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {

            LinkButton lb = ((LinkButton)sender);
            DataControlFieldCell celula = (DataControlFieldCell)lb.Parent;
            GridViewRow linha = (GridViewRow)celula.Parent;

            int id = Convert.ToInt32(linha.Cells[0].Text);

            PacoteBOMetodos pBO = new PacoteBOMetodos();

            PacoteBO p = new PacoteBO();
            p = pBO.consultar(id);
            
            pBO.remover(p);
            GridView1.DataBind();

        }

       

    }
}
